package com.mdd.admin.service.subject.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.subject.ISubjectCourseService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.subject.SubjectCourseBatchParam;
import com.mdd.admin.validate.subject.SubjectCourseParam;
import com.mdd.admin.vo.subject.SubjectCourseListVo;
import com.mdd.admin.vo.subject.SubjectCourseDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.subject.Subject;
import com.mdd.common.entity.subject.SubjectCourse;
import com.mdd.common.mapper.course.CourseMapper;
import com.mdd.common.mapper.subject.SubjectCourseMapper;
import com.mdd.common.mapper.subject.SubjectMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 专题专区课程实现类
 * @author panweiliang
 */
@Service
public class SubjectCourseServiceImpl implements ISubjectCourseService {
        
    @Resource
    SubjectCourseMapper subjectCourseMapper;

    @Resource
    SubjectMapper subjectMapper;

    @Resource
    CourseMapper courseMapper;


    /**
     * 专题专区课程新增
     *
     * @author panweiliang
     * @param subjectCourseParam 参数
     */
    @Override
    public void add(SubjectCourseParam subjectCourseParam) {
        Integer subjectId = subjectCourseParam.getSubjectId();
        Integer courseId = subjectCourseParam.getCourseId();
        Subject subject = subjectMapper.selectOne(
                new QueryWrapper<Subject>()
                        .eq("id", subjectId)
                        .last("limit 1"));
        Assert.notNull(subject, "专题/专区数据不存在!");
        Course course = courseMapper.selectOne(
                new QueryWrapper<Course>()
                        .eq("id", courseId)
                        .last("limit 1"));
        Assert.notNull(course, "课程数据不存在!");

        SubjectCourse subjectCourse = subjectCourseMapper.selectOne(
                new QueryWrapper<SubjectCourse>()
                        .eq("subject_id", subjectId)
                        .eq("course_id", courseId)
                        .last("limit 1"));
        Assert.isNull(subjectCourse, "当前专题/专区已经引用了该课程");

        SubjectCourse model = new SubjectCourse();
        model.setSubjectId(subjectId);
        model.setCourseId(courseId);
        subjectCourseMapper.insert(model);
    }

    /**
     * 专题专区课程新增
     *
     * @author panweiliang
     * @param subjectCourseBatchParam 参数
     */
    @Override
    public void addInBulk(SubjectCourseBatchParam subjectCourseBatchParam) {
        Integer subjectId = subjectCourseBatchParam.getSubjectId();
        List<Integer> courseIds = subjectCourseBatchParam.getCourseIds();
        Subject subject = subjectMapper.selectOne(
                new QueryWrapper<Subject>()
                        .eq("id", subjectId)
                        .last("limit 1"));
        Assert.notNull(subject, "专题/专区数据不存在!");


        for (Integer courseId : courseIds) {
            Course course = courseMapper.selectOne(
                    new QueryWrapper<Course>()
                            .eq("id", courseId)
                            .last("limit 1"));
            Assert.notNull(course, "课程数据不存在!");

//            SubjectCourse subjectCourse = subjectCourseMapper.selectOne(
//                    new QueryWrapper<SubjectCourse>()
//                            .eq("subject_id", subjectId)
//                            .eq("course_id", courseId)
//                            .last("limit 1"));
//            Assert.isNull(subjectCourse, "当前专题/专区已经引用了该课程");
        }

        subjectCourseMapper.delete(new QueryWrapper<SubjectCourse>().eq("subject_id", subjectId));

        for (Integer courseId : courseIds) {
            SubjectCourse model = new SubjectCourse();
            model.setSubjectId(subjectId);
            model.setCourseId(courseId);
            subjectCourseMapper.insert(model);
        }

    }




    /**
     * 专题专区课程删除
     *
     * @author panweiliang
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        SubjectCourse model = subjectCourseMapper.selectOne(
                new QueryWrapper<SubjectCourse>()
                    .eq("id", id)
                    .last("limit 1"));
        Assert.notNull(model, "数据不存在!");
        subjectCourseMapper.delete(new QueryWrapper<SubjectCourse>().eq("id", id));
    }

    /**
     * 专题专区课程删除
     *
     * @author panweiliang
     * @param courseId 课程ID
     * @param subjectId 专题/专区id
     */
    @Override
    public void del(Integer courseId, Integer subjectId) {
        SubjectCourse model = subjectCourseMapper.selectOne(
                new QueryWrapper<SubjectCourse>()
                        .eq("subject_id", subjectId)
                        .eq("course_id", courseId)
                        .last("limit 1"));
        Assert.notNull(model, "数据不存在!");
        subjectCourseMapper.delete(new QueryWrapper<SubjectCourse>().eq("subject_id", subjectId).eq("course_id", courseId));
    }

}
